<template>
  <div class="list-table">
    <div class="area-header">
      <span class="area-header-title">{{ $t('租金计划') }}</span>
    </div>
    <el-form ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item :label="$t('合同编号')" prop="contractId">
        <el-input
          v-model="queryParams.contractId"
          :placeholder="$t('合同编号')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('期次')" prop="issue">
        <el-input
          v-model="queryParams.issue"
          :placeholder="$t('期次')"
          clearable
          @keyup.enter.native="queryHandle"
        />
      </el-form-item>
      <el-form-item :label="$t('付款截止日期')" prop="paymentDueDate">
        <el-date-picker
          v-model="dateRange"
          style="width: 400px"
          type="daterange"
          align="right"
          unlink-panels
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="queryHandle">{{ $t('搜索') }}</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">{{ $t('重置') }}</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['leasingProject:leaseRentPlan:edit']"
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="addHandle"
        >{{ $t('新增') }}</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          v-hasPermi="['leasingProject:leaseRentPlan:import']"
          type="info"
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >{{ $t('导入') }}
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dealLeaseRentPlanList" border tooltip-effect="darkTable">
      <el-table-column :label="$t('序号')" type="index" align="center" show-overflow-tooltip width="50">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column :label="$t('合同编号')" align="center" prop="contractNo" min-width="180">
        <template slot-scope="scope">
          <el-link type="primary" @click="openTask(scope.row)">{{ scope.row.contractNo }}</el-link>
        </template>
      </el-table-column>
      <el-table-column :label="$t('期次')" align="center" prop="issue" min-width="180" />
      <el-table-column :label="$t('租期')" align="center" prop="leaseStartDate" min-width="280">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.leaseStartDate) + ' - ' + parseTime(scope.row.leaseEndDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('付款截止日期')" align="center" prop="paymentDueDate" min-width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paymentDueDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('期末租金余额') + $t('金额单位')" align="center" prop="balanceEndingRent" min-width="180">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.balanceEndingRent, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('固定租金') + $t('金额单位')" align="center" prop="deadRent" min-width="180">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.deadRent, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('浮动租金') + $t('金额单位')" align="center" prop="floatingRent" min-width="180">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.floatingRent, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('租金总金额') + $t('金额单位')" align="center" prop="totalAmountReceivable" min-width="180">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.totalAmountReceivable, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('实际收款日期')" align="center" prop="actualPayoutDate" min-width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.actualPayoutDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('实际收款金额') + $t('金额单位')" align="center" prop="actualCollectionRent" min-width="180">
        <template slot-scope="scope">
          <span>{{ moneyFormat(scope.row.actualCollectionRent, 'w') }}</span>
        </template>
      </el-table-column>
      <el-table-column :label="$t('责任人')" align="center" prop="nickName" min-width="180" />
      <el-table-column :label="$t('操作')" align="center" class-name="small-padding fixed-width" min-width="200" fixed="right">
        <template slot-scope="scope">
          <el-button
            v-hasPermi="['leasingProject:leaseRentPlan:edit']"
            size="small"
            type="text"
            icon="el-icon-edit"
            @click="updateHandle(scope.row)"
          >{{ $t('修改') }}</el-button>
          <el-button
            v-hasPermi="['leasingProject:leaseRentPlan:remove']"
            size="small"
            type="text"
            icon="el-icon-delete"
            @click="deleteHandle(scope.row)"
          >{{ $t('删除') }}</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改租赁项目-租金计划对话框 -->
    <el-dialog :title="$t(title)" :visible.sync="formOpen" :close-on-click-modal="false" :lock-scroll="false" custom-class="dialogForm" append-to-body>
      <dealLeaseRentPlanForm ref="dealLeaseRentPlanForm" />
    </el-dialog>
    <!-- 导入对话框 -->
    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px" :close-on-click-modal="false" :lock-scroll="false" append-to-body>
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport+ '&dealId=' + upload.dealId"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload" />
        <div class="el-upload__text">
          {{ $t('将文件拖到此处，或') }}
          <em>{{ $t('点击上传') }}</em>
        </div>
        <div slot="tip" class="el-upload__tip" style="color:red">{{ $t('提示：仅允许导入“xls”或“xlsx”格式文件！') }}</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="info" class="el-icon-download el-icon--right" style="font-size:12px;float:left;" @click="fetchTemplate">
          {{ $t('下载模板') }}
        </el-button>
        <el-button @click="upload.open = false">{{ $t("取消") }}</el-button>
        <el-button type="primary" @click="submitFileForm">{{ $t("确定") }}</el-button>
      </div>
    </el-dialog>
    <el-dialog :visible.sync="formOpen2" :title="$t('流程详情')" :destroy-on-close="true" custom-class="dialogForm" style=" text-align: left;" append-to-body :close-on-click-modal="false" :lock-scroll="false">
      <ProcessInfoForm ref="processInfoForm" />
    </el-dialog>
  </div>
</template>

<script>
import { listDealLeaseRentPlan, delDealLeaseRentPlan, exportDealLeaseRentPlan } from '@/api/deal/leasingProject/dealLeaseRentPlan'
import dealLeaseRentPlanForm from './dealLeaseRentPlanForm.vue'
import { getToken } from '@/utils/auth'
import ProcessInfoForm from '@/views/act/process/processInfoForm'
export default {
    components: {
        dealLeaseRentPlanForm,
      ProcessInfoForm
    },
  data() {
      return {
          dateRange: [],
          // 遮罩层
          loading: true,
          // 总条数
          total: 0,
          // 租赁项目-租金计划表格数据
          dealLeaseRentPlanList: [],
          // 弹出层标题
          title: '租金计划',
          // 是否显示弹出层
          formOpen: false,
          formOpen2: false,
          // 查询参数
          queryParams: {
              dealId: this.$route.query.id,
              contractId: undefined,
              issue: undefined,
              paymentDueDate: undefined,
              beginTime: undefined,
              endTime: undefined,
              pageNum: 1,
              pageSize: 10
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {},
        // 导入参数
        upload: {
          // 是否显示弹出层
          open: false,
          // 弹出层标题
          title: '',
          // 是否禁用上传
          isUploading: false,
          // 是否更新已经存在的用户数据
          updateSupport: 0,
          dealId: this.$route.query.id,
          // 设置上传的请求头部
          headers: { Authorization: 'Bearer ' + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + '/leaseRentPlan/dealLeaseRentPlan/importData'
        }
      }
  },
  created() {
      this.getList()
  },
  methods: {
    /** 查询租赁项目-租金计划列表 */
    getList() {
        this.loading = true
      if (this.dateRange != null && this.dateRange.length > 1) {
        this.queryParams.beginTime = this.parseTime(this.dateRange[0])
        this.queryParams.endTime = this.parseTime(this.dateRange[1])
      } else {
        this.queryParams.startDate = undefined
        this.queryParams.endDate = undefined
      }
        listDealLeaseRentPlan(this.queryParams).then(response => {
            this.dealLeaseRentPlanList = response.rows
            this.total = response.total
            this.loading = false
        }).catch(() => {
            this.loading = false
        })
    },

    // 表单重置
    reset() {
        this.form = {
            id: undefined,
            dealId: undefined,
            contractId: undefined,
            issue: undefined,
            leaseStartDate: undefined,
            leaseEndDate: undefined,
            paymentDueDate: undefined,
            balanceEndingRent: undefined,
            deadRent: undefined,
            floatingRent: undefined,
            serviceCharge: undefined,
            totalAmountReceivable: undefined,
            actualPayoutDate: undefined,
            actualCollectionRent: undefined,
            userId: undefined,
            remark: undefined,
            delFlag: undefined,
            createBy: undefined,
            createTime: undefined,
            updateBy: undefined,
            updateTime: undefined
        }
        this.resetForm('form')
    },
    /** 搜索按钮操作 */
    queryHandle() {
        this.queryParams.pageNum = 1
        this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = []
      this.queryParams.beginTime = undefined
      this.queryParams.endTime = undefined
        this.resetForm('queryForm')
        this.queryHandle()
    },
    /** 新增按钮操作 */
    addHandle() {
        this.formOpen = true
        this.$nextTick(() => {
            this.$refs.dealLeaseRentPlanForm.init(null)
        })
    },
    /** 修改按钮操作 */
    updateHandle(row) {
        this.formOpen = true
        this.$nextTick(() => {
            // 调用form页面的 init 方法初始化数据
            this.$refs.dealLeaseRentPlanForm.init(row)
        })
    },
    /** 删除按钮操作 */
    deleteHandle(row) {
        this.$confirm(this.$t('是否确认删除?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(() => {
            return delDealLeaseRentPlan(row.id)
        }).then(() => {
            this.getList()
            this.msgSuccess(this.$t('删除成功'))
        }).catch(() => {
            this.loading = false
        })
    },
    /** 导出按钮操作 */
    exportHandle() {
        const queryParams = this.queryParams
        this.$confirm(this.$t('是否确认导出数据项?'), this.$t('警告'), {
            confirmButtonText: this.$t('确定'),
            cancelButtonText: this.$t('取消'),
            type: 'warning'
        }).then(() => {
            return exportDealLeaseRentPlan(queryParams)
        }).then(response => {
            this.download(response.msg)
        }).catch(() => {
        })
    },
    // 导入
    handleImport() {
      this.upload.title = this.$t('导入')
      this.upload.open = true
    },
    // 文件上传中处理
    handleFileUploadProgress() {
      this.upload.isUploading = true
    },
    // 文件上传成功处理
    handleFileSuccess(response) {
      this.upload.open = false
      this.upload.isUploading = false
      this.$refs.upload.clearFiles()
      this.$alert(response.msg, this.$t('导入结果'), { dangerouslyUseHTMLString: true })
      this.getList()
    },
    // 下载模板
    fetchTemplate() {
      var href1 = window.location.href
      var href2 = this.$route.path
      var url = href1.substring(0, href1.indexOf(href2))
      window.location.href = url + '/template/dealLeaseRentPlanImportTemplates.xlsx'
    },
    // 提交上传文件
    submitFileForm() {
      this.$refs.upload.submit()
    },
    // 打开待办页面
    openTask(row) {
      this.formOpen2 = true
      this.$nextTick(() => {
        this.$refs.processInfoForm.init(row)
      })
    }
  }
}
</script>
